package sfmc.biz.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

import sfmc.biz.service.SfmcCountStatusService;
import sfmc.biz.vo.Sfmc_TB_STATUS;
import sfmc.common.util.DateUtil;

@Controller
public class SfmcCountStatusController {

	protected Log log = LogFactory.getLog(this.getClass());
	
	@Autowired 
	private SfmcCountStatusService sfmcCountStatusService;
	
	
	@RequestMapping(value="/daillyStauts_list.do")
	public ModelAndView daillyStatusList(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_STATUS sfmc_TB_STATUS){
		
		if("".equals(sfmc_TB_STATUS.getSearchFromDate())){
			sfmc_TB_STATUS.setSearchFromDate(DateUtil.getCurrentTime("yyyy-MM-dd"));
			sfmc_TB_STATUS.setSearchToDate(DateUtil.getCurrentTime("yyyy-MM-dd"));
		}
		
		List<Sfmc_TB_STATUS> daillyStatusList = sfmcCountStatusService.getDailyStatusList(sfmc_TB_STATUS);
		
		mav.addObject("daillyStatusList", (new Gson()).toJson(daillyStatusList));
		mav.addObject("daillstatusdata", sfmc_TB_STATUS);
		mav.setViewName("/countstatus/daillystatus_list");

		return mav;
	}
	
	@RequestMapping(value="/weeklyStatus_list.do")
	public ModelAndView WeeklyStatusList(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_STATUS sfmc_TB_STATUS){
		
		if("".equals(sfmc_TB_STATUS.getSearchFromDate())){
			sfmc_TB_STATUS.setSearchFromDate(DateUtil.getCurrentTime("yyyy-MM-dd"));
			sfmc_TB_STATUS.setSearchToDate(DateUtil.getCurrentTime("yyyy-MM-dd"));
		}
		
		List<Sfmc_TB_STATUS> weeklyStatusList = sfmcCountStatusService.getWeeklyStatusList(sfmc_TB_STATUS);
		
		mav.addObject("weeklyStatusList" , (new Gson()).toJson(weeklyStatusList));
		mav.addObject("weeklystatusdata", sfmc_TB_STATUS);
		mav.setViewName("/countstatus/weeklystatus_list");
		
		return mav;
	}

	
	@RequestMapping(value="/yearlyStauts_list.do")
	public ModelAndView yearlyStatusList(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_STATUS sfmc_TB_STATUS){
		
		if("".equals(sfmc_TB_STATUS.getSearchFromDate())){
			sfmc_TB_STATUS.setSearchFromDate(DateUtil.getCurrentTime("yyyy"));
			sfmc_TB_STATUS.setSearchToDate(DateUtil.getCurrentTime("yyyy"));
		}
		
		List<Sfmc_TB_STATUS> yearlyStatusList = sfmcCountStatusService.getYearlyStatusList(sfmc_TB_STATUS);
		
		mav.addObject("yearlyStatusList", (new Gson()).toJson(yearlyStatusList));
		mav.addObject("yearlystatusdata", sfmc_TB_STATUS);
		mav.setViewName("/countstatus/yearlystatus_list");

		return mav;
	}
	
	@RequestMapping(value="/monthlyStauts_list.do")
	public ModelAndView monthlyStatusList(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_STATUS sfmc_TB_STATUS){
		
		if("".equals(sfmc_TB_STATUS.getSearchFromDate())){
			sfmc_TB_STATUS.setSearchFromDate(DateUtil.getCurrentTime("yyyy-MM"));
			sfmc_TB_STATUS.setSearchToDate(DateUtil.getCurrentTime("yyyy-MM"));
		}
		
		List<Sfmc_TB_STATUS> monthlyStatusList = sfmcCountStatusService.getMonthlyStatusList(sfmc_TB_STATUS);
		
		mav.addObject("monthlyStatusList", (new Gson()).toJson(monthlyStatusList));
		mav.addObject("monthlystatusdata", sfmc_TB_STATUS);
		mav.setViewName("/countstatus/monthlystatus_list");

		return mav;
	}
	
	@RequestMapping(value="/daillystatus_excel.do")
	public void listsToExcel(HttpServletResponse resp, OutputStream ost,ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_STATUS sfmc_TB_STATUS){
		
		FileOutputStream fos = null;
		
		List<Sfmc_TB_STATUS> excelList = sfmcCountStatusService.getDailyStatusList(sfmc_TB_STATUS);
		resp.setHeader("Content-Disposition", "attachment; filename=DaillyStatus.xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet firstSheet  = workbook.createSheet("sheet1");
		
		for(int i=0; i<excelList.size(); i++){
			
			HSSFRow rowHeader = firstSheet.createRow(0);
			HSSFCell headerA = rowHeader.createCell(0);
			HSSFCell headerB = rowHeader.createCell(1);
			headerA.setCellValue(new HSSFRichTextString("주문날짜"));
			headerB.setCellValue(new HSSFRichTextString("주문건수"));
			
			
			sfmc_TB_STATUS = excelList.get(i);
			HSSFRow rowA = firstSheet.createRow(i+1);
			HSSFCell cells1 = rowA.createCell(0); 
			HSSFCell cells2 = rowA.createCell(1);
			cells1.setCellValue(sfmc_TB_STATUS.getOrdDate());
			cells2.setCellValue(sfmc_TB_STATUS.getStatusCount());
		}
		
		try{
			workbook.write(ost);
			ost.flush();
			fos = new FileOutputStream(new File("C:/DaillyStatus.xls"));
			workbook.write(fos);
		}catch(Exception e){
			log.error(e);
		}finally{
			if(fos != null){
				try{
					fos.flush();
					fos.close();
				}
				catch(IOException e){
					log.error(e);
				}
			}
		}
	}
	
	@RequestMapping(value="/weeklystatus_excel.do")
	public void weeklistsToExcel(HttpServletResponse resp, OutputStream ost,ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_STATUS sfmc_TB_STATUS){
		
		FileOutputStream fos = null;
		
		List<Sfmc_TB_STATUS> excelList = sfmcCountStatusService.getWeeklyStatusList(sfmc_TB_STATUS);
		resp.setHeader("Content-Disposition", "attachment; filename=WeeklyStatus.xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet firstSheet  = workbook.createSheet("sheet1");
		
		for(int i=0; i<excelList.size(); i++){
			
			HSSFRow rowHeader = firstSheet.createRow(0);
			HSSFCell headerA = rowHeader.createCell(0);
			HSSFCell headerB = rowHeader.createCell(1);
			HSSFCell headerC = rowHeader.createCell(2);
			HSSFCell headerD = rowHeader.createCell(3);
			headerA.setCellValue(new HSSFRichTextString("년도"));
			headerB.setCellValue(new HSSFRichTextString("월"));
			headerC.setCellValue(new HSSFRichTextString("주"));
			headerD.setCellValue(new HSSFRichTextString("주문건수"));
			
			sfmc_TB_STATUS = excelList.get(i);
			HSSFRow rowA = firstSheet.createRow(i+1);
			HSSFCell cells1 = rowA.createCell(0); 
			HSSFCell cells2 = rowA.createCell(1);
			HSSFCell cells3 = rowA.createCell(2);
			HSSFCell cells4 = rowA.createCell(3);
			cells1.setCellValue(sfmc_TB_STATUS.getOrdDate());
			cells2.setCellValue(sfmc_TB_STATUS.getOrdSubstrM());
			cells3.setCellValue(sfmc_TB_STATUS.getOrdSubstrW());
			cells4.setCellValue(sfmc_TB_STATUS.getStatusCount());
		}

		try{
			workbook.write(ost);
			ost.flush();
			fos = new FileOutputStream(new File("C:/WeeklyStatus.xls"));
			workbook.write(fos);
		}catch(Exception e){
			log.error(e);
		}finally{
			if(fos != null){
				try{
					fos.flush();
					fos.close();
				}
				catch(IOException e){
					log.error(e);
				}
			}
		}
	}
	
	@RequestMapping(value="/monthlyStauts_excel.do")
	public void monthlistsToExcel(HttpServletResponse resp, OutputStream ost,ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_STATUS sfmc_TB_STATUS){
		
		FileOutputStream fos = null;
		
		List<Sfmc_TB_STATUS> excelList = sfmcCountStatusService.getMonthlyStatusList(sfmc_TB_STATUS);
		resp.setHeader("Content-Disposition", "attachment; filename=MonthlyStatus.xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet firstSheet  = workbook.createSheet("sheet1");
		
		for(int i=0; i<excelList.size(); i++){
			
			HSSFRow rowHeader = firstSheet.createRow(0);
			HSSFCell headerA = rowHeader.createCell(0);
			HSSFCell headerB = rowHeader.createCell(1);
			HSSFCell headerC = rowHeader.createCell(2);
			headerA.setCellValue(new HSSFRichTextString("주문년도"));
			headerB.setCellValue(new HSSFRichTextString("주문월"));
			headerC.setCellValue(new HSSFRichTextString("주문건수"));
			
			sfmc_TB_STATUS = excelList.get(i);
			HSSFRow rowA = firstSheet.createRow(i+1);
			HSSFCell cells1 = rowA.createCell(0); 
			HSSFCell cells2 = rowA.createCell(1);
			HSSFCell cells3 = rowA.createCell(2);
			cells1.setCellValue(sfmc_TB_STATUS.getOrdDate());
			cells2.setCellValue(sfmc_TB_STATUS.getOrdDateM());
			cells3.setCellValue(sfmc_TB_STATUS.getStatusCount());
		}
		
		try{
			workbook.write(ost);
			ost.flush();
			fos = new FileOutputStream(new File("C:/MonthlyStatus.xls"));
			workbook.write(fos);
		}catch(Exception e){
			log.error(e);
		}finally{
			if(fos != null){
				try{
					fos.flush();
					fos.close();
				}
				catch(IOException e){
					log.error(e);
				}
			}
		}
	}
	
	
	@RequestMapping(value="/yearlyStauts_excel.do")
	public void yearlistsToExcel(HttpServletResponse resp, OutputStream ost,ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_STATUS sfmc_TB_STATUS){
		
		FileOutputStream fos = null;
		
		List<Sfmc_TB_STATUS> excelList = sfmcCountStatusService.getYearlyStatusList(sfmc_TB_STATUS);
		resp.setHeader("Content-Disposition", "attachment; filename=yearlyStatus.xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet firstSheet  = workbook.createSheet("sheet1");
		
		for(int i=0; i<excelList.size(); i++){
			
			HSSFRow rowHeader = firstSheet.createRow(0);
			HSSFCell headerA = rowHeader.createCell(0);
			HSSFCell headerB = rowHeader.createCell(1);
			headerA.setCellValue(new HSSFRichTextString("주문년도"));
			headerB.setCellValue(new HSSFRichTextString("주문건수"));
			
			sfmc_TB_STATUS = excelList.get(i);
			HSSFRow rowA = firstSheet.createRow(i+1);
			HSSFCell cells1 = rowA.createCell(0); 
			HSSFCell cells2 = rowA.createCell(1);
			cells1.setCellValue(sfmc_TB_STATUS.getOrdDate());
			cells2.setCellValue(sfmc_TB_STATUS.getStatusCount());
		}
		
		try{
			workbook.write(ost);
			ost.flush();
			fos = new FileOutputStream(new File("C:/yearlyStatus.xls"));
			workbook.write(fos);
		}catch(Exception e){
			log.error(e);
		}finally{
			if(fos != null){
				try{
					fos.flush();
					fos.close();
				}
				catch(IOException e){
					log.error(e);
				}
			}
		}
	}
}
